package com.duing.controller;

import com.duing.bean.DataBean;
import com.duing.bean.GraphBean;
import com.duing.bean.MapBean;
import com.duing.handler.GraphHandler;
import com.duing.service.DataService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
public class DataController {

    @Autowired
    private DataService dataService;

    //表格
    @GetMapping("/")
    public String list(Model model){
        List<DataBean> beanList = dataService.list();
        model.addAttribute("beanList",beanList);

        graph(model);
        map(model);
        return "list";
    }

    //折线图
    @GetMapping("/graph")
    public String graph(Model model){
        List<GraphBean> beans = GraphHandler.getGraphData();
        ArrayList<String> dateList = new ArrayList<>();
        ArrayList<Integer> confirmList = new ArrayList<>();
        ArrayList<Integer> suspectList = new ArrayList<>();

        for (int i = 0; i < beans.size(); i++) {
             GraphBean graphBean = beans.get(i);
             dateList.add(graphBean.getDate());
             confirmList.add(graphBean.getConfirm());
             suspectList.add(graphBean.getSuspect());
        }

        model.addAttribute("dateList",dateList);
        model.addAttribute("confirmList",confirmList);
        model.addAttribute("suspectList",suspectList);

        return "graph";
    }


    //地图
    @GetMapping("/map")
    public String map(Model model){

        //获取所有数据
        List<DataBean> list = dataService.list();

        //分别存入现有数据和累计数据
        List<MapBean> result1 = new ArrayList<>();
        List<MapBean> result2 = new ArrayList<>();

        for (DataBean dataBean : list) {

            //现有确诊数据
            MapBean mapBean1 = new MapBean(dataBean.getArea(),dataBean.getNowConfirm());
            result1.add(mapBean1);

            //累计确诊数据
            MapBean mapBean2 = new MapBean(dataBean.getArea(),dataBean.getConfirm());
            result2.add(mapBean2);
        }

        //渲染
        model.addAttribute("mapBean1",new Gson().toJson(result1));
        model.addAttribute("mapBean2",new Gson().toJson(result2));

        return "map";
    }
}
